<template>
  <div class="xtx-goods-page" v-if="goods.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem :to="`/category/${goods.categories[1].id}`">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 【商品信息】 -->
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 【左侧】图片预览区 -->
        <div class="media">
          <XtxImageView :image-list="goods.mainPictures" />
          <GoodsSale :goods="goods" />
        </div>
        <!-- 【右侧】商品信息区 -->
        <div class="spec">
          <goods-info :goods="goods" />
          <goods-sku :goods="goods" @change="change" />
          <XtxNumbox v-model="count" :max="countMax" />
          <XtxButton
            type="primary"
            style="margin-top: 20px"
            @click="insertCart"
          >
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 【商品详情】 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsDetail :goods="goods" />
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" />
          <GoodsHot :type="2" />
          <GoodsHot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { computed, ref } from 'vue'
import { useRoute } from 'vue-router'
import GoodsSale from './components/goods-sale.vue'
import GoodsInfo from './components/goods-info.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import { message } from '@/components/Message'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsSale,
    GoodsInfo,
    GoodsSku,
    GoodsDetail,
    GoodsHot
  },
  setup() {
    const route = useRoute()
    const goods = ref({})
    const store = useStore()
    async function getGoods() {
      // RefImpl 需要 .value
      const res = await reqFindGoods(route.params.id)
      goods.value = res.result
    }
    getGoods()
    // 不要忘记把 goods 导出~
    const currSku = ref({})
    const change = (sku) => {
      if (sku.skuId) {
        // 更新价格和库存信息
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      currSku.value = sku
    }

    const count = ref(1)

    const countMax = computed(() => {
      return goods.value.inventory > 0 ? goods.value.inventory : 1
    })

    const insertCart = async () => {
      if (!currSku.value.skuId) {
        return message({ type: 'error', text: '请选择商品规格' })
      }
      if (count.value > currSku.value.inventory) {
        return message({ type: 'error', text: '库存不足' })
      }
      await store.dispatch('cart/asyncInsertCart', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true, // 选中
        isEffective: true, // 是否有效
        count: count.value // 商品数量
      })
      message({ type: 'success', text: '添加成功' })
    }
    return {
      goods,
      change,
      count,
      countMax,
      insertCart
    }
  }
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
